package com.ruoyi.wbs.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wbs.domain.Contractor;
import com.ruoyi.wbs.service.IContractorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 承包商Controller
 * 
 * @author renyuxin
 * @date 2024-06-19
 */
@RestController
@RequestMapping("/wbs/contractor")
public class ContractorController extends BaseController
{
    @Autowired
    private IContractorService contractorService;

    /**
     * 查询承包商列表
     */
    @PreAuthorize("@ss.hasPermi('wbs:contractor:list')")
    @GetMapping("/list")
    public TableDataInfo list(Contractor contractor)
    {
        startPage();
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = loginUser.getUserId();
//        List<Contractor> list = contractorService.selectContractorList(contractor);
        List<Contractor> list = contractorService.selectContractorListForUser(contractor, userId);
        return getDataTable(list);
    }

    /**
     * 导出承包商列表
     */
    @PreAuthorize("@ss.hasPermi('wbs:contractor:export')")
    @Log(title = "承包商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Contractor contractor)
    {
        List<Contractor> list = contractorService.selectContractorList(contractor);
        ExcelUtil<Contractor> util = new ExcelUtil<Contractor>(Contractor.class);
        util.exportExcel(response, list, "承包商数据");
    }

    /**
     * 获取承包商详细信息
     */
    @PreAuthorize("@ss.hasPermi('wbs:contractor:query')")
    @GetMapping(value = "/{contractorId}")
    public AjaxResult getInfo(@PathVariable("contractorId") Long contractorId)
    {
        return success(contractorService.selectContractorByContractorId(contractorId));
    }

    /**
     * 新增承包商
     */
    @PreAuthorize("@ss.hasPermi('wbs:contractor:add')")
    @Log(title = "承包商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Contractor contractor)
    {
        return toAjax(contractorService.insertContractor(contractor));
    }

    /**
     * 修改承包商
     */
    @PreAuthorize("@ss.hasPermi('wbs:contractor:edit')")
    @Log(title = "承包商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Contractor contractor)
    {
        return toAjax(contractorService.updateContractor(contractor));
    }

    /**
     * 删除承包商
     */
    @PreAuthorize("@ss.hasPermi('wbs:contractor:remove')")
    @Log(title = "承包商", businessType = BusinessType.DELETE)
	@DeleteMapping("/{contractorIds}")
    public AjaxResult remove(@PathVariable Long[] contractorIds)
    {
        return toAjax(contractorService.deleteContractorByContractorIds(contractorIds));
    }
}
